package cashpointer.mobile.model;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ExtendedCheckBoxListAdapter extends BaseAdapter {
	private Context context;
	private List<ExtendedCheckBox> items = new LinkedList<ExtendedCheckBox>();

	public ExtendedCheckBoxListAdapter(Context context) {
		this.context = context;
	}

     /**
      * Dodanie nowego wiersza
      * @param item
      */
	public void addItem(ExtendedCheckBox item) {
		items.add(item);
	}

	/**
	 * Ustawienie listy
	 */
	public void setListItems(List<ExtendedCheckBox> list) {
		items = list;
    }

	/**
	 * Zwraca wielkosc listy
	 */
    public int getCount() {
    	return items.size();
    }

    /**
     * Zwraca obiekt z wybranej pozycji
     */
    public Object getItem(int position) {
    	return items.get(position);
    }
   
    /**
     * Zwraca pozycje wybranego elementu
     */
    public int getPosition(ExtendedCheckBox item) {
    	int count = getCount();
    	for (int i = 0; i < count; i++) {
    		if (item.compareTo((ExtendedCheckBox)getItem(i)) == 0) {
    			return i;
    		}
        }
    	return -1;
    }

    /**
     * Ustawienie zaznaczenia na wybranej pozycji
     * @param value - true or false
     * @param position - position
     */
    public void setChecked(boolean value, int position) {
        items.get(position).setChecked(value);
    }

    /**
     * Zaznaczenie wszystkich elementow
     */
    public void selectAll() {
    	for (ExtendedCheckBox checkbox : items) {
    		checkbox.setChecked(true);
        }
       
        /* Things have changed, do a redraw. */
        this.notifyDataSetInvalidated();
    }

    /**
     * Zaznaczenie wszystkich elementow
     */
    public void selectReverse() {
    	for (ExtendedCheckBox checkbox : items) {
    		if (checkbox.getChecked()) {
    			checkbox.setChecked(false);
        	} else {
    			checkbox.setChecked(true);
    		}
        }
       
        /* Things have changed, do a redraw. */
        this.notifyDataSetInvalidated();
    }
   
    /**
     * Odznacznie wszystkich elementow
     */
    public void deselectAll() {
    	for (ExtendedCheckBox checkbox : items) {
    		checkbox.setChecked(false);
        }
       
        /* Things have changed, do a redraw. */
        this.notifyDataSetInvalidated();
    }

    /**
     * Czy wszystkie cheackbox'y sa zanaczone?
     * @return - true or false
     */
    public boolean areAllItemsSelectable() {
    	return false;
    }

    /**
     * Use the array index as a unique id
     */
    public long getItemId(int position) {
         return position;
    }

    /**
     * Do not recycle a view if one is already there, if not the data could get corrupted and
     * the checkbox state could be lost.
     * @param convertView The old view to overwrite
     * @returns a CheckBoxifiedTextView that holds wraps around an CheckBoxifiedText */
    public View getView(int position, View convertView, ViewGroup parent ){
    	return new ExtendedCheckBoxListView(context, items.get(position));
    }
}
